﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlDoc = HtmlAgilityPack.HtmlDocument;
using WebFunctions;
using Newtonsoft.Json;
using System.IO;
using System.Windows.Forms;


namespace AutoIndustryFiles
{
    class danfossScraper : FileLinksScraper2
    {
        int Index = 0;
        public danfossScraper()
        {
            this.MainPage = "http://www.danfoss.com/China/Products/Categories/";
            this.CompanyName = "丹佛斯中国";
            this.CompanyUrl = "http://www.danfoss.com/China/Products/Categories/";
            this.ColumnName = "主页 > 产品 > 目录";
            //this.ReviseXPath += new OnReviseXPath(FuJiScraper_ReviseXPath);            
        }

        public void Traverse()
        {
            bool fv = false;
            Queue<string> urls = new Queue<string> { };
            List<string> urls_processed = new List<string> { };
            UpdateMsg("danfossScraper", CompanyUrl, ref fv);
            urls_processed.Add(CompanyUrl);
            string html = WebFuncs.OpenUrlEx(CompanyUrl, "UTF-8");
            string url = "";

            HtmlDoc doc = new HtmlDoc();
            doc.LoadHtml(html);

            foreach (HtmlAgilityPack.HtmlNode nodeLevel1 in doc.DocumentNode.SelectNodes(@"//td[@class=""leftSubMenuItem""]/input[1]"))
            {
                url = nodeLevel1.GetAttributeValue("value", "");
                url = FormatUrl(url, "http://www.danfoss.com/");
                if (!string.IsNullOrEmpty(url))
                    urls.Enqueue(url);
            }
            
            while (urls.Count > 0)
            {
                url = urls.Dequeue();
                UpdateMsg("danfossScraper",urls.Count.ToString() + ":" +  url, ref fv);

                if (urls_processed.Contains(url))
                    continue;
                else
                {
                    urls_processed.Add(url);
                    File.AppendAllText(Application.StartupPath + "\\Output\\urls.txt",url + Environment.NewLine);
                }

                html = WebFuncs.OpenUrlEx(url, "UTF-8");
                if (string.IsNullOrEmpty(html))
                    continue;

                doc.LoadHtml(html);
                
                if (doc == null)
                    continue;
                //如果有"产品线"则继续入队列
                if (doc.DocumentNode.SelectSingleNode(@"//a[@title=""产品线""]") != null) // and @class=""linktext1boldred""
                {
                    HtmlAgilityPack.HtmlNodeCollection hnc = doc.DocumentNode.SelectNodes(@"//td/a[@class=""linktext1bold""]");
                    if (hnc == null)
                        continue;
                    foreach (HtmlAgilityPack.HtmlNode productLink in hnc)
                    {
                        url = productLink.GetAttributeValue("href", "");
                        url = FormatUrl(url, "http://www.danfoss.com/");
                        if (!string.IsNullOrEmpty(url))
                            if (url.IndexOf("http://www.danfoss.com/China/") >= 0)
                                urls.Enqueue(url);
                    }
                }
                //否则，输出html
                else
                {
                    //File.WriteAllText(@"D:\外包项目\自动化资料下载\Output\danfoss\html\" + Index.ToString() + ".html", html, Encoding.GetEncoding("UTF-8"));
                    File.WriteAllText(Application.StartupPath +"\\Output\\" + Index.ToString() + ".html", html, Encoding.GetEncoding("UTF-8")); 
                    Index++;
                }
            }

            ////td/a[@class="linktext1bold"]
        }

        public override void GenerateAllLinks(string param)
        {

        }
    }
}
